package com.baway.biliclitent.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.baway.biliclitent.R;
import com.baway.biliclitent.adapter.MyTomAtoCenterAdapter;
import com.baway.biliclitent.adapter.MyTomAtoTopAdapter;
import com.baway.biliclitent.adapter.MyTomBotAdapter;
import com.baway.biliclitent.model.TomatoBean;
import com.baway.biliclitent.model.TomatoBotBean;
import com.baway.biliclitent.util.GlideImageLoader;
import com.baway.biliclitent.util.OkHttpUtils;
import com.baway.biliclitent.util.UrlUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * 番剧 Fragment
 */

public class FragmentTomato extends Fragment {

    private Banner mBanner;
    private OkHttpUtils httpUtils;
    private RecyclerView mTopRecy;
    private RecyclerView mBotRecy;
    private MyTomAtoTopAdapter myTomAtoTopAdapter;
    private RecyclerView mCenRecy;
    private MyTomAtoCenterAdapter myTomAtoCenterAdapter;
    private MyTomBotAdapter myTomBotAdapter;
    private SwipeRefreshLayout mSwipe;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fargment_tomato,container,false);
        initView(view);
        return view;
    }
    //初始化资源id
    private void initView(View view) {
        mSwipe = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefrech);
        mBanner = (Banner) view.findViewById(R.id.tomato_head_banner);
        //获取解析数据的对象
        httpUtils = OkHttpUtils.getHttpUtils();
        //获取上方和下方的recyclerView
        mTopRecy = (RecyclerView) view.findViewById(R.id.top_tomato_recycler);
        mCenRecy = (RecyclerView) view.findViewById(R.id.center_tomato_recycler);
        mBotRecy = (RecyclerView) view.findViewById(R.id.bottom_tomato_recycler);
        //设置管理者
        mTopRecy.setLayoutManager(new LinearLayoutManager(getContext()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mCenRecy.setLayoutManager(new LinearLayoutManager(getContext()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mBotRecy.setLayoutManager(new LinearLayoutManager(getContext()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        //设置适配器
        myTomAtoTopAdapter = new MyTomAtoTopAdapter(getContext());
        myTomAtoCenterAdapter = new MyTomAtoCenterAdapter(getContext());
        myTomBotAdapter = new MyTomBotAdapter(getContext());
        mTopRecy.setAdapter(myTomAtoTopAdapter);
        mCenRecy.setAdapter(myTomAtoCenterAdapter);
        mBotRecy.setAdapter(myTomBotAdapter);
        //设置条目动画
        mTopRecy.setItemAnimator(new DefaultItemAnimator());
        mBotRecy.setItemAnimator(new DefaultItemAnimator());
        mBotRecy.setItemAnimator(new DefaultItemAnimator());
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSwipe.setRefreshing(false);
                    }
                },200);
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //拼接头部的Banner以及下方的数据的url
        String bannerUrl = UrlUtils.DRAMA_HEAD+UrlUtils.DRAMA_DATA;
        String recommendUrl = UrlUtils.DRAMA_HEAD + UrlUtils.DRAMA_RECOMMEND;
        //解析上方布局的方法
        httpUtils.loadDataFromNet(bannerUrl, TomatoBean.class, new OkHttpUtils.CallBackListener<TomatoBean>() {
            @Override
            public void onSuccess(TomatoBean result) {
                //回调方法成功的里面首先取到象征着banner图片的集合
                List<String> imageList = new ArrayList<String>();
                List<TomatoBean.ResultBean.AdBean.HeadBean> image = result.getResult().getAd().getHead();
                for(int i=0;i<image.size();i++){
                    imageList.add(image.get(i).getImg());
                }
                TomatoBanner(imageList);
                //取出十月新番的集合
                List<TomatoBean.ResultBean.PreviousBean.ListBean> octoberList = result.getResult().getPrevious().getList();
                //取出新番连载的集合
                List<TomatoBean.ResultBean.SerializingBean> serializingList = result.getResult().getSerializing();
                //设置数据
                myTomAtoTopAdapter.setData(serializingList);
                myTomAtoCenterAdapter.setData(octoberList);
            }

            @Override
            public void onFail() {
                Toast.makeText(getContext(),"亲，网络解析错误",Toast.LENGTH_SHORT).show();return;
            }
        });
        //解析番剧推荐的数据
        httpUtils.loadDataFromNet(recommendUrl, TomatoBotBean.class, new OkHttpUtils.CallBackListener<TomatoBotBean>() {
            @Override
            public void onSuccess(TomatoBotBean result) {
                List<TomatoBotBean.ResultBean> resultList = result.getResult();
                myTomBotAdapter.setData(resultList);
            }

            @Override
            public void onFail() {

            }
        });
    }


    //设置图片轮播图的方法
    public void TomatoBanner(List<String> images){
        //设置图片加载器
        mBanner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        mBanner.setImages(images);
        //设置小圆点的位置
        mBanner.setIndicatorGravity(BannerConfig.RIGHT);
        //banner里面的方法  调用完毕后调用
        mBanner.start();
    }
}
